package com.bianmaba.beans.datacollection;

import com.alibaba.fastjson.JSONObject;
import com.bianmaba.spring.data.jpa.domain.annotations.QueryCacheable;
import com.bianmaba.spring.data.jpa.domain.entity.UuidIdentityEntity;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import java.util.HashMap;
import java.util.List;


@Entity
@DynamicUpdate()
@DynamicInsert()
//此注解重新定义为是否开始查询缓存
@QueryCacheable
//此注解表示此类支持二级缓存，缓存名称为：defaultEntityCache
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Data
@NoArgsConstructor
public class CollectionTask extends UuidIdentityEntity {
    private String name;
    private String description;
    private String parameters;
    private String projectId;

    @ManyToOne(optional = false)
    @JoinColumn(name = "collection_interface_id")
    private CollectionInterface collectionInterface;

    public static CollectionTask of(String taskId) {
        CollectionTask task = new CollectionTask();
        task.setId(taskId);
        return task;
    }

    public List<HashMap> getParameterMaps() {
        List<HashMap> map = JSONObject.parseArray(this.parameters, HashMap.class);
        return map;
    }
}
